package com.aaa.controller.systemmanage;

import com.aaa.aspect.LogMonitor;
import com.aaa.controller.BaseController;
import com.aaa.entity.PageSortEntity;
import com.aaa.entity.Result;
import com.aaa.entity.systemmanage.User;
import com.aaa.service.systemmanage.UserService;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 部门表(Dept)表控制层
 *
 * @author makejava
 * @since 2022-08-30 19:45:01
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    //查询所有
    @RequestMapping(method = RequestMethod.GET, value = "/queryAll.do")
    public Result queryAll(PageSortEntity pageSortEntity, User user) {
        Page pageResult = userService.queryAll(pageSortEntity, user);
        return success(pageResult.getResult(), pageResult.getTotal());
    }

    //添加用户
    @LogMonitor
    @RequestMapping("/insert.do")
    public Result insert(User user) {
        boolean user2 = userService.insert(user);
        if (user2) {
            return success("添加成功");
        } else {
            return fail("添加失败");
        }
    }

    //删除用户和批量删除
    @LogMonitor
    @RequestMapping("/delete.do")
    public Result delete(String[] ids) {
        //把要删除userid添加到list中，方便后端获取
        List list = new ArrayList();
        for (String id : ids) {
            list.add(id);
        }
        boolean user1 = userService.deleteUser(list);
        if (user1) {
            return success();
        } else {
            return fail();
        }
    }

    //修改用户
    @LogMonitor
    @RequestMapping("/update.do")
    public Result update(User user) {
        boolean user2 = userService.update(user);
        if (user2) {
            return success();
        } else {
            return fail();
        }
    }

    //头像上传
    @RequestMapping("/upload.do")
    @ResponseBody
    public Result upload(MultipartFile file) throws IOException, ServletException {
        //获取原始文件名
        String filename = file.getOriginalFilename();
        //System.out.println("获取到了文件名:"+filename);
        //获取随机码
        UUID uuid = UUID.randomUUID();
        //将随机码和后缀拼接，组成的新的文件名
        String objectName = uuid + filename;
        //System.out.println("新的文件名："+objectName);
        Result success = null;
        try {
            // Endpoint以杭州为例，其它Region请按实际情况填写。
            String endpoint = "oss-cn-hangzhou.aliyuncs.com";
            // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，
            // 创建并使用RAM子账号进行API访问或日常运维
            String accessKeyId = "LTAI5tKQM5g9cMzG5SkFm2nZ";
            String accessKeySecret = "MzX1QJUTDwIcVwrnAUyjWBQC1oIoZ4";
            String bucketName = "myfirstuploadoss";

            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(file.getBytes()));
            // 关闭OSSClient。
            ossClient.shutdown();
            String url = "https://" + bucketName + "." + endpoint + "/" + objectName;
            success = success(url);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return success;
    }


}

